#include "simi2c.h"
#include "myoled.h"
#include "delay.h"
#include "mydht11.h"
#include "myadc.h"
#include "stdio.h"
#include "bsp_exti.h"
#include "bsp_led.h" 
#include "sys.h"
#include "ds3231.h"  // 包含RTC驱动



char guangzhao[30];
u8 temp;
u8 humi;
char wendu[30];
char shidu[30];
extern int start;
//extern int start1;

// 蜂鸣器响三声的函数
void BEEP_SoundThreeTimes() {
    int i;  // 将变量声明移到函数开头
    for (i = 0; i < 3; i++) {  // 循环三次
        // 打开蜂鸣器
        TIM_SetCompare3(TIM2, 300);  // 设置占空比
        TIM_Cmd(TIM2, ENABLE);
        delay_ms(500);  // 蜂鸣器响500ms

        // 关闭蜂鸣器
        TIM_Cmd(TIM2, DISABLE);
        delay_ms(500);  // 间隔500ms
    }
}

	u8 paint [] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
	0x80,0xC0,0x40,0x40,0x60,0x20,0x30,0x10,0x18,0x08,0x08,0x08,0x08,0x0C,0x04,
	0x04,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x8C,0x88,0x88,0x98,0x90,0x90,
	0xB0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x60,0x38,
	0x18,0x0C,0x04,0xC6,0x42,0x43,0x21,0x21,0x30,0x10,0x18,0x18,0x18,0x0C,0x8C,
	0xC4,0xE4,0xE6,0xF2,0xE2,0x83,0x01,0x01,0x01,0x01,0x00,0xF0,0xFC,0xFC,0xFC,
	0xFC,0xF0,0xF0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xC1,0xC1,0xC2,0xC6,0x9C,
	0x30,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x80,0xF0,0x1C,0x06,0xC3,0xE0,0x30,0x18,0x0C,0xE6,0xF3,0xF0,0xF0,0xF8,0xF0,
	0xF8,0xF8,0xF8,0xFC,0xFE,0xFE,0xFF,0xFF,0x7F,0x7F,0xBF,0xBF,0xDF,0x4F,0x40,
	0xC0,0x00,0x00,0x00,0x03,0x03,0x87,0xE7,0xE7,0x27,0x27,0x27,0x27,0x27,0x27,
	0x27,0x27,0x27,0x27,0x67,0x47,0xC7,0x87,0x87,0x00,0x03,0x0E,0x18,0x60,0x40,
	0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x03,0x00,0x00,0xF0,0x1F,0x01,0x00,
	0x00,0x00,0x01,0x01,0xE1,0x21,0x31,0x11,0x11,0x11,0x99,0xC9,0xED,0x74,0x76,
	0xF2,0x9B,0x89,0x8C,0x84,0x84,0xFC,0x9C,0x98,0xF0,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0xF8,0xFC,0x7E,0x3E,0x6F,0x65,0xEF,0xBB,0xA1,0xA3,0x22,0x76,0xCC,
	0xCC,0xF9,0xF1,0x81,0x81,0x06,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x03,0x02,
	0x06,0x0C,0x08,0x38,0x20,0x60,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0xF0,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x01,0x1F,0x3F,0x1D,0x0C,0x0C,0x0F,0x2C,0x67,0xC2,0x82,0x83,0x01,
	0x01,0x01,0x19,0x11,0x31,0x60,0x40,0xC0,0x80,0x00,0x00,0x08,0x18,0x30,0x60,
	0x40,0x80,0x80,0x04,0x0D,0x19,0x11,0x31,0x60,0x40,0xC0,0x80,0x00,0x04,0x18,
	0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
	0x03,0x0E,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x90,0xB8,
	0x48,0x98,0x70,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x01,0x02,0x06,0x04,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x7E,0xC0,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x03,0x02,0x07,0x8E,0xCC,0x79,0x32,0x35,0x1B,0x1E,
	0x18,0x10,0x30,0x60,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x80,0x0C,0x18,0x30,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x80,0xF8,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x18,0x1C,0x10,0x7C,
	0x1E,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x02,0x27,0x66,0xFE,
	0x7E,0x64,0xFC,0xF0,0xA0,0xE0,0xC0,0x00,0x00,0x00,0x01,0x43,0x22,0x00,0x00,
	0x87,0xFE,0x10,0x10,0x30,0x20,0x60,0x40,0x60,0x30,0x18,0x08,0x0C,0x1F,0x34,
	0x60,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x60,0x30,0x1C,0x07,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00


};
void SSD1306_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]);

int main()
{
	//延时函数
	delay_init(168);
	//iic
	IIC_Init();
	//oled
	InitOLed();
	//adc
	myAdcInit();
	//温湿度传感器
	//DHT11_Init();
	//蜂鸣器技术
	Init_BEEP();
	TIM12_PWM_CH2_FAN_CONFIG();
	// 舵机初始化
    TIM4_PWM_CH1_MOTOR_CONFIG();
	//按键中断
	EXTI_Key_Config();
	//gpio初始化
	LED_GPIO_Config();
	//GPIO_ResetBits(GPIOB,LED3_PIN);
	

	
	SSD1306_DrawBMP(0,0,128,8,paint);
	delay_ms(700);
	OLed_Fill(0x00);
	while(1)
	{
		if(start == 1)
		{	
			DHT11_Init();
			
			OLed_ShowTemp();
			OLed_ShowLight();
			sprintf(guangzhao,"%d",getAdcAverage(0,5));
			delay_ms(500);
			OLed_ShowASCII(40,0,guangzhao);
			
			DHT11_Read_Data(&temp,&humi);	
			sprintf(wendu,"%d",temp);
			sprintf(shidu,"%d",humi);	
			OLed_ShowASCII(40,2,wendu);
			OLed_ShowASCII(40,4,shidu);
		}
		
		if(start == 0)
		{
			offInitOLed();
		}
		//光照过低，大于3000，led3亮
		if(getAdcAverage(0,4)>3000)
		{
			GPIO_ResetBits(GPIOE, LED3_PIN);
      MOTO_RIGHT();
		}
		else
	 {
      
		 GPIO_SetBits(GPIOE, LED3_PIN); 
		 
   }

		
		if(temp>35)
		{
			
			Fan_Control(10000);
			BEEP_SoundThreeTimes();
		}else
		{
			
			Fan_Stop();
			
		}
		
		if(humi>60)
		{
			
			Fan_Control(10000);
			  MOTO_RIGHT();
		}else
		{
			
			Fan_Stop();
			
		}
		 // 读取当前时间并显示
          int year, month, day, hour, minute;
        DS3231_ReadTime(&year, &month, &day, &hour, &minute);
        char time_str[30];
        sprintf(time_str, "%04d-%02d-%02d %02d:%02d", year, month, day, hour, minute);
        OLed_ShowASCII(0, 6, time_str);  // 在OLED上显示时间
	}

}
